import axios from 'axios'

export default {
  namespaced: true,
  state: {
    list: []
  },
  getters: {
    totalCount (state) {
      return state.list.reduce((pre, cur) => {
        return pre + cur.count
      }, 0)
    },
    totalPrice (state) {
      return state.list.reduce((pre, cur) => {
        return pre + cur.count * cur.price
      }, 0)
    }
  },
  mutations: {
    saveList (state, payload) {
      state.list = payload
    }
  },
  actions: {
    async loadData (store) {
      const res = await axios({ url: 'http://localhost:3000/cart' })
      console.log(res)
      store.commit('saveList', res.data)
    }
  }
}
